package com.carkeeper.subject.controller;

import com.carkeeper.common.utils.PageResult;
import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.pojo.StoreUser;
import com.carkeeper.subject.service.StoreUserService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author chengxihua
 * @version 1.0
 * @date 2020/11/25 16:01
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块API")
@CrossOrigin
public class StoreUserController {

    @Autowired
    private StoreUserService storeUserService;

    /**
     * 查询审核状态成功信息
     *
     * @return
     */
    @GetMapping("/reviewSuccess/{page}/{size}")
    @ApiOperation("查询审核状态成功信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searMap",value = "条件"),
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示")
    })
    public Result findStoreSuccessByAuditStatus(@RequestParam Map searMap, @PathVariable("page") int page, @PathVariable("size") int size) {
        Page<StoreUser> pageList = storeUserService.findStoreSuccessByAuditStatus(searMap, page, size);
        if (pageList != null) {
            return new Result(true, StatusCode.OK, "查询成功", pageList);
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }


    /**
     * 创建管理员
     *
     * @param storeUser
     * @return
     */
    @ApiOperation("创建管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeUser",value = "用户表信息")
    })
    @PutMapping("/insertUser")
    public Result insertStoreUserByStoreCode(@RequestBody StoreUser storeUser) {
        int count = storeUserService.insertStoreUser(storeUser);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "添加成功");
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }


    /**
     * 重置密码
     *
     * @param id
     * @param password
     * @return
     */
    @ApiOperation("重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户ID"),
            @ApiImplicitParam(name = "password",value = "密码")
    })
    @RequestMapping(value = "/resetPassword")
    public Result resetPasswordsByUserId(@PathVariable Long id, @RequestParam String password) throws Exception {
        int count = storeUserService.resetPasswordsByUserId(id, password);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "重置成功");
        }
        return new Result(false, StatusCode.ERROR, "重置失败");
    }

    /**
     * 查询所有用户信息
     *
     * @return
     */

    @ApiOperation("查询所有用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searMap",value = "条件"),
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示")
    })
    @GetMapping("/findUser/{page}/{size}")
    public Result findAllStoreUser(@RequestParam Map searMap, @PathVariable int page, @PathVariable int size) {
        Page<StoreUser> pageList = storeUserService.findAllStoreUser(searMap, page, size);
        if (pageList != null) {
            return new Result(true, StatusCode.OK, "查询成功", pageList);
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }


    /**
     * 锁定用户
     *
     * @param id
     * @return
     */

    @ApiOperation("锁定用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户ID")
    })
    @PostMapping(value = "/lock")
    public Result lockUser(@RequestParam Long id) {
        int count = storeUserService.lockUser(id);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "锁定成功");
        }
        return new Result(false, StatusCode.ERROR, "锁定失败");
    }


}
